package com.shu.juc;

import java.util.Random;
import java.util.concurrent.*;

/**
 * @author ssm
 * @version V1.0
 * @description:
 * @date 2020/7/30 10:01
 */

public class FutureTaskTest {


    public static void main(String[] args) throws InterruptedException, ExecutionException, TimeoutException {
        ExecutorService service = Executors.newFixedThreadPool(10);
//        Future<Integer> future = service.submit(new CallableTask());
//        System.out.println(future.get());

        FutureTask task = new FutureTask(new CallableTask());
        service.submit(task);
        System.out.println(task.get(2, TimeUnit.SECONDS));

    }

    static class CallableTask implements Callable<Integer> {
        @Override
        public Integer call() throws Exception {
            Thread.sleep(2000);
            System.out.println("执行完了");
            return new Random().nextInt();
        }
    }
}
